package org.example.wikwok.service.impl;

import org.example.wikwok.common.ReturnedMessage;
import org.example.wikwok.mapper.CommentMapper;
import org.example.wikwok.mapper.OperationLogMapper;
import org.example.wikwok.mapper.Secondary_CommentMapper;
import org.example.wikwok.mapper.UserMapper;
import org.example.wikwok.pojo.Comment;
import org.example.wikwok.pojo.CommentInfo;
import org.example.wikwok.pojo.OperationLog;
import org.example.wikwok.pojo.Secondary_Comment;
import org.example.wikwok.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//提供评论的查看逻辑。
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentMapper commentMapper;
@Autowired
private UserMapper userMapper;
@Autowired
private Secondary_CommentMapper secondary_CommentMapper;
@Autowired
private OperationLogMapper operationLogMapper;

    @Override
    public List<CommentInfo<Comment>> comments(int userId, String videoId) {
        try{
            //根据用户id和视频id查询评论
            List<Comment> comments = commentMapper.SelectByVideoId(videoId);
            //将评论信息封装成CommentInfo对象
            List<CommentInfo<Comment>> commentInfos = new ArrayList<>();
            for(Comment comment : comments){
                commentInfos.add(new CommentInfo<>(comment,userMapper.selectByPrimaryKey(comment.getUserid())));
            }
            operationLogMapper.insert(OperationLog.build("User list comment  ", userId, ReturnedMessage.COMMENTLISTSUCCESS));
            return commentInfos;
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User list comment  ", userId, ReturnedMessage.COMMENTLISTFAILED));
            return null;
        }
    }

    @Override
    public List<CommentInfo<Secondary_Comment>> secendComments(int userId, String videoId, String commentId) {
        try{
            int commentid=Integer.parseInt(commentId);
            List<Secondary_Comment> secondary_Comments = secondary_CommentMapper.selectByCommentId(commentid);
            List<CommentInfo<Secondary_Comment>> commentInfos = new ArrayList<>();
            for(Secondary_Comment secondary_Comment : secondary_Comments){
                commentInfos.add(new CommentInfo<>(secondary_Comment,userMapper.selectByPrimaryKey(secondary_Comment.getUserid())));
            }
            operationLogMapper.insert(OperationLog.build("User list secendcomment  ", userId, ReturnedMessage.SECENDCOMMENTLISTSUCCESS));
            return commentInfos;
        }catch(Exception e){
            operationLogMapper.insert(OperationLog.build("User list secendcomment  ", userId, ReturnedMessage.SECENDCOMMENTLISTFAILED));
            return null;
        }
    }
}
